//
//  CallCenterManager+Layout.m
//  Financial
//
//  Created by SuperChao.liang on 2016/11/29.
//  Copyright © 2016年 ZYT.com. All rights reserved.
//

#import "CallCenterManager+Layout.h"
#import "Masonry.h"

#define kToolBallWidth                              50
#define kToolBallHeight                             50
#define kScreenWidth                                [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight                               [[UIScreen mainScreen] bounds].size.height

@implementation CallCenterManager (Layout)

/**
 *  window层
 */
- (void)initTooleBar
{
    __weak __typeof(&*self)weakSelf = self;
    // 默认不显示callbtn
    self.isShowToolsBar = NO;
    
    // window层
    self.window = [[UIWindow alloc] initWithFrame:CGRectMake(100, 100, kToolBallWidth, kToolBallHeight)];
    self.window.windowLevel = UIWindowLevelAlert + 5;
    self.window.backgroundColor = [UIColor clearColor];
    // 拖动手势
    self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.window addGestureRecognizer:self.panGesture];

    // 初始化功能键盘
    BaseBtn *baseView = [BaseBtn buttonWithType:UIButtonTypeCustom];
    baseView.layer.cornerRadius = kToolBallWidth/2;
    self.callStateBtn.backgroundColor = [UIColor grayColor];
    [self.window addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.window);
        make.right.mas_equalTo(weakSelf.window.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(kToolBallWidth, kToolBallHeight));
    }];
    
    // 背景图片
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"callToolBarBG.png"]] ;
    self.backgroundView.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
    self.backgroundView.backgroundColor = [UIColor clearColor];
    [baseView addSubview:self.backgroundView];
    
    // 取消
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
    self.cancelBtn.tag = 2;
    self.cancelBtn.layer.cornerRadius = kToolBallWidth/2;
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.backgroundColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:0.5];
    [baseView addSubview:self.cancelBtn];
    
    // 保持
    self.holdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.holdBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
    self.holdBtn.tag = 3;
    self.holdBtn.layer.cornerRadius = kToolBallWidth/2;
    [self.holdBtn setTitle:@"保持" forState:UIControlStateNormal];
    [self.holdBtn addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventTouchUpInside];
    self.holdBtn.backgroundColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:0.5];
    [baseView addSubview:self.holdBtn];
    
    // 外放
    self.loudspeakerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loudspeakerBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
    self.loudspeakerBtn.tag = 4;
    self.loudspeakerBtn.layer.cornerRadius = kToolBallWidth/2;
    [self.loudspeakerBtn setTitle:@"扬声" forState:UIControlStateNormal];
    [self.loudspeakerBtn addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventTouchUpInside];
    self.loudspeakerBtn.backgroundColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:0.5];
    [baseView addSubview:self.loudspeakerBtn];
    
    // 状态球（主球）
    self.callStateBtn = [BaseBtn buttonWithType:UIButtonTypeCustom];
    self.callStateBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
    self.callStateBtn.tag = 1;
    self.callStateBtn.layer.cornerRadius = kToolBallWidth/2;
    [self.callStateBtn setTitle:@"状态" forState:UIControlStateNormal];
    [self.callStateBtn addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventTouchUpInside];
    self.callStateBtn.backgroundColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:1];
    [baseView addSubview:self.callStateBtn];
}

- (void)setToolsBarState
{
    __weak __typeof(&*self)weakSelf = self;
    __block BOOL isShow = self.isShowToolsBar;
    CGPoint point = self.window.frame.origin;
    // 计算window变化后的尺寸差
    CGFloat gapX = kToolBallWidth*2 - kToolBallWidth;
    CGFloat gapY = (kToolBallWidth*3 - kToolBallWidth)/2;
    
    if(isShow)
    {
        // 调整Window尺寸
        self.window.frame = CGRectMake(point.x - gapX, point.y - gapY, kToolBallWidth*2, kToolBallHeight*3);
        // 调整toolBar
        [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            weakSelf.backgroundView.frame = CGRectMake(0, 0, kToolBallWidth*4, kToolBallWidth*4);
            weakSelf.backgroundView.center = self.callStateBtn.center;
            
            weakSelf.cancelBtn.frame = CGRectMake(-kToolBallWidth/2, - kToolBallWidth, kToolBallWidth, kToolBallWidth);
            weakSelf.holdBtn.frame = CGRectMake(-kToolBallWidth, 0, kToolBallWidth, kToolBallWidth);
            weakSelf.loudspeakerBtn.frame = CGRectMake(-kToolBallWidth/2, kToolBallWidth, kToolBallWidth, kToolBallWidth);
        } completion:nil];
    }
    else
    {
        // 调整Window尺寸
        self.window.frame = CGRectMake(point.x + gapX, point.y + gapY, kToolBallWidth, kToolBallHeight);
        // 调整toolBar
        [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            weakSelf.backgroundView.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
            
            weakSelf.cancelBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
            weakSelf.holdBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
            weakSelf.loudspeakerBtn.frame = CGRectMake(0, 0, kToolBallWidth, kToolBallWidth);
        } completion:nil];
    }
}

#pragma mark - action

- (void)clickHandle:(UIButton *)btn
{
    NSInteger tag = btn.tag;
    switch (tag) {
        case 1:
            self.isShowToolsBar = !self.isShowToolsBar;
            [self setToolsBarState];
            break;
        case 2:
            NSLog(@"取消");
            break;
        case 3:
            NSLog(@"保持");
            break;
        case 4:
            NSLog(@"扬声");
            break;
        default:
            break;
    }
}

#pragma mark - Pan
/**
 *  处理拖动
 */
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    __weak __typeof(&*self)weakSelf = self;
    
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        // 如果toolBar处于打开状态，先关闭，再进行移动
        if(self.isShowToolsBar)
        {
            self.isShowToolsBar = !self.isShowToolsBar;
            [self setToolsBarState];
        }
    }
    else if(recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [recognizer translationInView:self.window];
        // 控制小球范围
        if((self.window.center.x + translation.x <= kToolBallWidth/2) && (self.window.center.y + translation.y <= kToolBallWidth/2))
        {// 左上
            
            recognizer.view.center = CGPointMake(kToolBallWidth/2, kToolBallHeight/2);
        }
        else if ((self.window.center.x + translation.x <= kToolBallWidth/2) && (self.window.center.y + translation.y >= kScreenHeight - kToolBallHeight/2))
        {// 左下
            recognizer.view.center = CGPointMake(kToolBallWidth/2, kScreenHeight - kToolBallHeight/2);
        }
        else if ((self.window.center.x + translation.x >= kScreenWidth - kToolBallWidth/2) && (self.window.center.y + translation.y <= kToolBallHeight/2))
        {// 右上
            recognizer.view.center = CGPointMake(kScreenWidth - kToolBallWidth/2, kToolBallHeight/2);
        }
        else if ((self.window.center.x + translation.x >= kScreenWidth - kToolBallWidth/2) && (self.window.center.y + translation.y >= kScreenHeight - kToolBallHeight/2))
        {// 右下
            recognizer.view.center = CGPointMake(kScreenWidth - kToolBallWidth/2, kScreenHeight - kToolBallHeight/2);
        }
        else if(self.window.center.x + translation.x <= kToolBallWidth/2)
        {// 左
            recognizer.view.center = CGPointMake(kToolBallWidth/2, recognizer.view.center.y + translation.y);
        }
        else if(self.window.center.x + translation.x >= kScreenWidth - kToolBallWidth/2)
        {// 右
            recognizer.view.center = CGPointMake(kScreenWidth - kToolBallWidth/2, recognizer.view.center.y + translation.y);
        }
        else if(self.window.center.y + translation.y <= kToolBallHeight/2)
        {// 上
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, kToolBallHeight/2);
        }
        else if(self.window.center.y + translation.y >= kScreenHeight - kToolBallHeight/2)
        {// 下
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, kScreenHeight - kToolBallHeight/2);
        }
        else
        {// 正常
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
        }
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.window];
    }
    else if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        CGPoint targetPoint;
        // 判断吸附哪边（暂时不需要此功能）
//        if(recognizer.view.frame.origin.x > kScreenWidth/2)
//        {
//            targetPoint = CGPointMake(kScreenWidth - kToolBallWidth/2, recognizer.view.center.y);
//        }
//        else
//        {
//            targetPoint = CGPointMake(kToolBallWidth/2, recognizer.view.center.y);
//        }
        targetPoint = CGPointMake(kScreenWidth - kToolBallWidth/2, recognizer.view.center.y);
        [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:5 options:UIViewAnimationOptionLayoutSubviews animations:^{
            recognizer.view.center = targetPoint;
        } completion:^(BOOL finished) {
            // 吸边动画结束后展开toolbar
            weakSelf.isShowToolsBar = !weakSelf.isShowToolsBar;
            [weakSelf setToolsBarState];
        }];
    }
}



@end
